import { h } from 'vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// 从路由生成菜单配置
export const generateMenuFromRoutes = (routes) => {
  const menus = []
  
  routes.forEach(route => {
    if (route.meta && !route.meta.hideInMenu) {
      const menu = {
        path: route.path,
        title: route.meta.title || '',
        icon: route.meta.icon || 'Document',
        children: []
      }
      
      if (route.children) {
        menu.children = generateMenuFromRoutes(route.children)
      }
      
      menus.push(menu)
    }
  })
  
  return menus
}

// 渲染图标组件
export const renderIcon = (icon) => {
  if (typeof icon === 'string' && ElementPlusIconsVue[icon]) {
    return () => h(ElementPlusIconsVue[icon])
  }
  return null
}

// 获取第一个可用的子路由
export const getFirstAvailableRoute = (routes) => {
  for (const route of routes) {
    if (!route.meta?.hideInMenu) {
      if (route.children) {
        const childRoute = getFirstAvailableRoute(route.children)
        if (childRoute) return childRoute
      }
      return route
    }
  }
  return null
}

// 获取当前路由的父级路径
export const getParentPath = (path, routes) => {
  for (const route of routes) {
    if (route.children) {
      for (const child of route.children) {
        if (child.path === path) {
          return route.path
        }
      }
      const found = getParentPath(path, route.children)
      if (found) return found
    }
  }
  return ''
} 